{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import log\n",
    "import operator\n",
    "def create():\n",
    "# 年龄：0代表青年，1代表中年，2代表老年；\n",
    "# 有工作：0代表否，1代表是；\n",
    "# 有自己的房子：0代表否，1代表是；\n",
    "# 信贷情况：0代表一般，1代表好，2代表非常好；\n",
    "# 类别(是否给贷款)：no代表否，yes代表是。\n",
    "    dataSet = [[0, 0, 0, 0, 'no'],         #数据集\n",
    "            [0, 0, 0, 1, 'no'],\n",
    "            [0, 1, 0, 1, 'yes'],\n",
    "            [0, 1, 1, 0, 'yes'],\n",
    "            [0, 0, 0, 0, 'no'],\n",
    "            [1, 0, 0, 0, 'no'],\n",
    "            [1, 0, 0, 1, 'no'],\n",
    "            [1, 1, 1, 1, 'yes'],\n",
    "            [1, 0, 1, 2, 'yes'],\n",
    "            [1, 0, 1, 2, 'yes'],\n",
    "            [2, 0, 1, 2, 'yes'],\n",
    "            [2, 0, 1, 1, 'yes'],\n",
    "            [2, 1, 0, 1, 'yes'],\n",
    "            [2, 1, 0, 2, 'yes'],\n",
    "            [2, 0, 0, 0, 'no']]\n",
    "    labels = ['年龄', '有工作', '有自己的房子', '信贷情况']#分类属性\n",
    "    return dataSet,labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算经验熵\n",
    "def cal_shamoo(dataset):\n",
    "    dataset_len=len(dataset)\n",
    "    labelcounts={}\n",
    "    for i in dataset:\n",
    "        label=i[-1]\n",
    "        if label not in labelcounts.keys():\n",
    "            labelcounts[label]=0\n",
    "        labelcounts[label]+=1\n",
    "    shamoo=0.0\n",
    "    print(labelcounts)\n",
    "    for i in labelcounts:\n",
    "        pro=float(labelcounts[i])/dataset_len\n",
    "        shamoo-=pro*log(pro,2)\n",
    "    return shamoo\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'no': 6, 'yes': 9}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.9709505944546686"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset,label=create()\n",
    "s=cal_shamoo(dataset)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'no': 6, 'yes': 9}\n",
      "{'no': 3, 'yes': 2}\n",
      "{'no': 2, 'yes': 3}\n",
      "{'yes': 4, 'no': 1}\n",
      "0个信息熵为0.083007\n",
      "{'no': 6, 'yes': 4}\n",
      "{'yes': 5}\n",
      "1个信息熵为0.323650\n",
      "{'no': 6, 'yes': 3}\n",
      "{'yes': 6}\n",
      "2个信息熵为0.419973\n",
      "{'no': 4, 'yes': 1}\n",
      "{'no': 2, 'yes': 4}\n",
      "{'yes': 4}\n",
      "3个信息熵为0.362990\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(0.4199730940219749, 2)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def split_data(data,axis,value):\n",
    "    data_ret=[]\n",
    "    for i in data:\n",
    "        if i[axis]==value:\n",
    "            temp=i[0:axis]\n",
    "            temp.extend(i[axis+1:])\n",
    "            data_ret.append(temp)\n",
    "    return data_ret\n",
    "\n",
    "def choose_best(data):\n",
    "    feature_counts=len(data[0])-1\n",
    "    base=cal_shamoo(data)\n",
    "    best=0.0\n",
    "    best_anix=-1\n",
    "    for i in range(feature_counts):\n",
    "        featList = [example[i] for example in data]\n",
    "        uniq=set(featList)\n",
    "        new_pro=0.0\n",
    "        for value in uniq:\n",
    "            after=split_data(data,i,value)\n",
    "            pro=len(after)/float(len(data))\n",
    "            new_pro+=pro*cal_shamoo(after)\n",
    "        infogain=base-new_pro\n",
    "        print('%i个信息熵为%f'%(i,infogain))\n",
    "        if infogain>best:\n",
    "            best=infogain\n",
    "            best_anix=i\n",
    "    return best,best_anix\n",
    "choose_best(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "def majorityCnt(classList):\n",
    "    classCount = {}\n",
    "    for vote in classList:                                        #统计classList中每个元素出现的次数\n",
    "        if vote not in classCount.keys():classCount[vote] = 0   \n",
    "        classCount[vote] += 1\n",
    "    sortedClassCount = sorted(classCount.items(), key = operator.itemgetter(1), reverse = True)        #根据字典的值降序排序\n",
    "    return sortedClassCount[0][0]                                #返回classList中出现次数最多的元素\n",
    "def createTree(dataSet, labels, featLabels):\n",
    "    classList = [example[-1] for example in dataSet]            #取分类标签(是否放贷:yes or no)\n",
    "    if classList.count(classList[0]) == len(classList):            #如果类别完全相同则停止继续划分\n",
    "        return classList[0]\n",
    "    if len(dataSet[0]) == 1:                                    #遍历完所有特征时返回出现次数最多的类标签\n",
    "        return majorityCnt(classList)\n",
    "    bestFeat = choose_best(dataSet)                #选择最优特征\n",
    "    bestFeatLabel = labels[bestFeat]                            #最优特征的标签\n",
    "    featLabels.append(bestFeatLabel)\n",
    "    myTree = {bestFeatLabel:{}}                                    #根据最优特征的标签生成树\n",
    "    del(labels[bestFeat])                                        #删除已经使用特征标签\n",
    "    featValues = [example[bestFeat] for example in dataSet]        #得到训练集中所有最优特征的属性值\n",
    "    uniqueVals = set(featValues)                                #去掉重复的属性值\n",
    "    for value in uniqueVals:                                    #遍历特征，创建决策树。                       \n",
    "        myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), labels, featLabels)\n",
    "    return myTree\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-50-a19061581ae6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmajorityCnt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdataset\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-47-6bb1d792d0d5>\u001b[0m in \u001b[0;36mmajorityCnt\u001b[1;34m(classList)\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mclassCount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mvote\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mclassList\u001b[0m\u001b[1;33m:\u001b[0m                                        \u001b[1;31m#统计classList中每个元素出现的次数\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m         \u001b[1;32mif\u001b[0m \u001b[0mvote\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mclassCount\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mclassCount\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mvote\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m         \u001b[0mclassCount\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mvote\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[0msortedClassCount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msorted\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mclassCount\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0moperator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitemgetter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreverse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m        \u001b[1;31m#根据字典的值降序排序\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "majorityCnt(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "classList ['no', 'no', 'yes', 'yes', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']\n",
      "no\n",
      "dataSet[0] [0, 0, 0, 0, 'no']\n",
      "classList ['no', 'no', 'yes', 'no', 'no', 'no', 'yes', 'yes', 'no']\n",
      "no\n",
      "dataSet[0] [0, 0, 0, 'no']\n",
      "classList ['no', 'no', 'no', 'no', 'no', 'no']\n",
      "no\n",
      "dataSet[0] [0, 0, 'no']\n",
      "classList ['yes', 'yes', 'yes']\n",
      "yes\n",
      "dataSet[0] [0, 1, 'yes']\n",
      "classList ['yes', 'yes', 'yes', 'yes', 'yes', 'yes']\n",
      "yes\n",
      "dataSet[0] [0, 1, 0, 'yes']\n",
      "放贷\n",
      "{'有自己的房子': {0: {'有工作': {0: 'no', 1: 'yes'}}, 1: 'yes'}}\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: UTF-8 -*-\n",
    "from math import log\n",
    "import operator\n",
    "\n",
    "\"\"\"\n",
    "函数说明:计算给定数据集的经验熵(香农熵)\n",
    "Parameters:\n",
    "    dataSet - 数据集\n",
    "Returns:\n",
    "    shannonEnt - 经验熵(香农熵)\n",
    "\"\"\"\n",
    "def calcShannonEnt(dataSet):\n",
    "    numEntires = len(dataSet)#返回数据集的行数\n",
    "    labelCounts = {}#保存每个标签(Label)出现次数的字典\n",
    "    for featVec in dataSet: #对每组特征向量进行统计\n",
    "        currentLabel = featVec[-1]                    #提取标签(Label)信息\n",
    "        if currentLabel not in labelCounts.keys():    #如果标签(Label)没有放入统计次数的字典,添加进去\n",
    "            labelCounts[currentLabel] = 0\n",
    "        labelCounts[currentLabel] += 1                #Label计数\n",
    "    shannonEnt = 0.0                                #经验熵(香农熵)\n",
    "    for key in labelCounts:                            #计算香农熵\n",
    "        prob = float(labelCounts[key]) / numEntires    #选择该标签(Label)的概率\n",
    "        shannonEnt -= prob * log(prob, 2)            #利用公式计算\n",
    "    return shannonEnt                                #返回经验熵(香农熵)\n",
    "\n",
    "\"\"\"\n",
    "函数说明:创建测试数据集\n",
    "\n",
    "Parameters:\n",
    "    无\n",
    "Returns:\n",
    "    dataSet - 数据集\n",
    "    labels - 特征标签\n",
    "Author:\n",
    "    Jack Cui\n",
    "Blog:\n",
    "    http://blog.csdn.net/c406495762\n",
    "Modify:\n",
    "    2017-07-20\n",
    "\"\"\"\n",
    "def createDataSet():\n",
    "    dataSet = [[0, 0, 0, 0, 'no'],                        #数据集\n",
    "            [0, 0, 0, 1, 'no'],\n",
    "            [0, 1, 0, 1, 'yes'],\n",
    "            [0, 1, 1, 0, 'yes'],\n",
    "            [0, 0, 0, 0, 'no'],\n",
    "            [1, 0, 0, 0, 'no'],\n",
    "            [1, 0, 0, 1, 'no'],\n",
    "            [1, 1, 1, 1, 'yes'],\n",
    "            [1, 0, 1, 2, 'yes'],\n",
    "            [1, 0, 1, 2, 'yes'],\n",
    "            [2, 0, 1, 2, 'yes'],\n",
    "            [2, 0, 1, 1, 'yes'],\n",
    "            [2, 1, 0, 1, 'yes'],\n",
    "            [2, 1, 0, 2, 'yes'],\n",
    "            [2, 0, 0, 0, 'no']]\n",
    "    labels = ['年龄', '有工作', '有自己的房子', '信贷情况']        #特征标签\n",
    "    return dataSet, labels                             #返回数据集和分类属性\n",
    "\n",
    "\"\"\"\n",
    "函数说明:按照给定特征划分数据集\n",
    "\n",
    "Parameters:\n",
    "    dataSet - 待划分的数据集\n",
    "    axis - 划分数据集的特征\n",
    "    value - 需要返回的特征的值\n",
    "Returns:\n",
    "    无\n",
    "Author:\n",
    "    Jack Cui\n",
    "Blog:\n",
    "    http://blog.csdn.net/c406495762\n",
    "Modify:\n",
    "    2017-07-24\n",
    "\"\"\"\n",
    "def splitDataSet(dataSet, axis, value):       \n",
    "    retDataSet = []                                        #创建返回的数据集列表\n",
    "    for featVec in dataSet:                             #遍历数据集\n",
    "        if featVec[axis] == value:\n",
    "            reducedFeatVec = featVec[:axis]                #去掉axis特征\n",
    "            reducedFeatVec.extend(featVec[axis+1:])     #将符合条件的添加到返回的数据集\n",
    "            retDataSet.append(reducedFeatVec)\n",
    "    return retDataSet                                      #返回划分后的数据集\n",
    "\n",
    "\"\"\"\n",
    "函数说明:选择最优特征\n",
    "\n",
    "Parameters:\n",
    "    dataSet - 数据集\n",
    "Returns:\n",
    "    bestFeature - 信息增益最大的(最优)特征的索引值\n",
    "\"\"\"\n",
    "def chooseBestFeatureToSplit(dataSet):\n",
    "    numFeatures = len(dataSet[0]) - 1#特征数量\n",
    "    baseEntropy = calcShannonEnt(dataSet)#计算数据集的香农熵\n",
    "    bestInfoGain = 0.0#信息增益\n",
    "    bestFeature = -1#最优特征的索引值\n",
    "    for i in range(numFeatures):#遍历所有特征\n",
    "        #获取dataSet的第i个所有特征\n",
    "        featList = [example[i] for example in dataSet]\n",
    "        uniqueVals = set(featList)                         #创建set集合{},元素不可重复\n",
    "        newEntropy = 0.0                                  #经验条件熵\n",
    "        for value in uniqueVals:                         #计算信息增益\n",
    "            subDataSet = splitDataSet(dataSet, i, value)         #subDataSet划分后的子集\n",
    "            prob = len(subDataSet) / float(len(dataSet))           #计算子集的概率\n",
    "            newEntropy += prob * calcShannonEnt(subDataSet)     #根据公式计算经验条件熵\n",
    "        infoGain = baseEntropy - newEntropy                     #信息增益\n",
    "        # print(\"第%d个特征的增益为%.3f\" % (i, infoGain))            #打印每个特征的信息增益\n",
    "        if (infoGain > bestInfoGain):                             #计算信息增益\n",
    "            bestInfoGain = infoGain                             #更新信息增益，找到最大的信息增益\n",
    "            bestFeature = i                                     #记录信息增益最大的特征的索引值\n",
    "    return bestFeature                                             #返回信息增益最大的特征的索引值\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "函数说明:统计classList中出现此处最多的元素(类标签)\n",
    "\n",
    "Parameters:\n",
    "    classList - 类标签列表\n",
    "Returns:\n",
    "    sortedClassCount[0][0] - 出现此处最多的元素(类标签)\n",
    "Author:\n",
    "    Jack Cui\n",
    "Blog:\n",
    "    http://blog.csdn.net/c406495762\n",
    "Modify:\n",
    "    2017-07-24\n",
    "\"\"\"\n",
    "def majorityCnt(classList):\n",
    "    classCount = {}\n",
    "    for vote in classList:                                        #统计classList中每个元素出现的次数\n",
    "        if vote not in classCount.keys():classCount[vote] = 0   \n",
    "        classCount[vote] += 1\n",
    "    sortedClassCount = sorted(classCount.items(), key = operator.itemgetter(1), reverse = True)        #根据字典的值降序排序\n",
    "    return sortedClassCount[0][0]                                #返回classList中出现次数最多的元素\n",
    "\n",
    "\"\"\"\n",
    "函数说明:创建决策树\n",
    "\n",
    "Parameters:\n",
    "    dataSet - 训练数据集\n",
    "    labels - 分类属性标签\n",
    "    featLabels - 存储选择的最优特征标签\n",
    "Returns:\n",
    "    myTree - 决策树\n",
    "Author:\n",
    "    Jack Cui\n",
    "Blog:\n",
    "    http://blog.csdn.net/c406495762\n",
    "Modify:\n",
    "    2017-07-25\n",
    "\"\"\"\n",
    "def createTree(dataSet, labels, featLabels):\n",
    "    classList = [example[-1] for example in dataSet]            #取分类标签(是否放贷:yes or no)\n",
    "#     print('dataSet',dataSet)\n",
    "    print('classList',classList)\n",
    "    \n",
    "    print(classList[0])\n",
    "    print('dataSet[0]',dataSet[0])\n",
    "    if classList.count(classList[0]) == len(classList):         #如果类别完全相同则停止继续划分\n",
    "        return classList[0]\n",
    "    if len(dataSet[0]) == 1:                                    #遍历完所有特征时返回出现次数最多的类标签\n",
    "        print('true')\n",
    "        \n",
    "        return majorityCnt(classList)\n",
    "    bestFeat = chooseBestFeatureToSplit(dataSet)                #选择最优特征\n",
    "    bestFeatLabel = labels[bestFeat]                            #最优特征的标签\n",
    "    featLabels.append(bestFeatLabel)\n",
    "    myTree = {bestFeatLabel:{}}                                    #根据最优特征的标签生成树\n",
    "    del(labels[bestFeat])                                        #删除已经使用特征标签\n",
    "    featValues = [example[bestFeat] for example in dataSet]        #得到训练集中所有最优特征的属性值\n",
    "    uniqueVals = set(featValues)                                #去掉重复的属性值\n",
    "    for value in uniqueVals:                                    #遍历特征，创建决策树。                       \n",
    "        myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), labels, featLabels)\n",
    "    return myTree\n",
    "\n",
    "\"\"\"\n",
    "函数说明:使用决策树分类\n",
    "\n",
    "Parameters:\n",
    "    inputTree - 已经生成的决策树\n",
    "    featLabels - 存储选择的最优特征标签\n",
    "    testVec - 测试数据列表，顺序对应最优特征标签\n",
    "Returns:\n",
    "    classLabel - 分类结果\n",
    "Author:\n",
    "    Jack Cui\n",
    "Blog:\n",
    "    http://blog.csdn.net/c406495762\n",
    "Modify:\n",
    "    2017-07-25\n",
    "\"\"\"\n",
    "def classify(inputTree, featLabels, testVec):\n",
    "    firstStr = next(iter(inputTree))                                                        #获取决策树结点\n",
    "    secondDict = inputTree[firstStr]                                                        #下一个字典\n",
    "    featIndex = featLabels.index(firstStr)                                               \n",
    "    for key in secondDict.keys():\n",
    "        if testVec[featIndex] == key:\n",
    "            if type(secondDict[key]).__name__ == 'dict':\n",
    "                classLabel = classify(secondDict[key], featLabels, testVec)\n",
    "            else: classLabel = secondDict[key]\n",
    "    return classLabel\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    dataSet, labels = createDataSet()\n",
    "    featLabels = []\n",
    "    myTree = createTree(dataSet, labels, featLabels)\n",
    "    testVec = [0,1] #测试数据\n",
    "    result = classify(myTree, featLabels, testVec)\n",
    "    if result == 'yes':\n",
    "        print('放贷')\n",
    "    if result == 'no':\n",
    "        print('不放贷')\n",
    "    print(myTree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def majorityCnt(classList):\n",
    "    classCount = {}\n",
    "    for vote in classList:                                        #统计classList中每个元素出现的次数\n",
    "        if vote not in classCount.keys():classCount[vote] = 0   \n",
    "        classCount[vote] += 1\n",
    "    sortedClassCount = sorted(classCount.items(), key = operator.itemgetter(1), reverse = True)        #根据字典的值降序排序\n",
    "    return sortedClassCount[0][0]                                #返回classList中出现次数最多的元素\n",
    "classList=['1','1','0','0']\n",
    "majorityCnt(classList)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
